import unittest

from google.appengine.ext import ndb

from word_statistic import *


class WordStatisticTest(unittest.TestCase):
    def setUp(self):
        self.word_stat = WordStatistic()

    def test_hist_ttime_construct(self):
        h_tt = self.word_stat.hist_ttime
        stat_game_1 = TTimeToCnt(time_sec=1, cnt_guess=1)
        histogram_update(h_tt, stat_game_1)
        self.assertEqual(len(list(h_tt)), 1)
        self.assertEqual(h_tt[0].cnt_guess, 1)
        self.assertEqual(h_tt[0].time_sec, 1)
        self.assertEqual(h_tt[0].cnt_fail, 0)
        self.assertEqual(h_tt[0].cnt_no_result, 0)

        stat_game_2 = TTimeToCnt(time_sec=1, cnt_guess=1)
        histogram_update(h_tt, stat_game_2)
        self.assertEqual(len(list(h_tt)), 1)
        self.assertEqual(h_tt[0].cnt_guess, 2)
        self.assertEqual(h_tt[0].time_sec, 1)
        self.assertEqual(h_tt[0].cnt_fail, 0)
        self.assertEqual(h_tt[0].cnt_no_result, 0)

        stat_game_3 = TTimeToCnt(time_sec=1, cnt_fail=1)
        histogram_update(h_tt, stat_game_3)
        self.assertEqual(len(list(h_tt)), 1)
        self.assertEqual(h_tt[0].cnt_guess, 2)
        self.assertEqual(h_tt[0].time_sec, 1)
        self.assertEqual(h_tt[0].cnt_fail, 1)
        self.assertEqual(h_tt[0].cnt_no_result, 0)

        stat_game_4 = TTimeToCnt(time_sec=2, cnt_guess=3,
                                 cnt_fail=2, cnt_no_result=1)
        histogram_update(h_tt, stat_game_4)
        self.assertEqual(len(list(h_tt)), 2)
        self.assertEqual(h_tt[0].cnt_guess, 2)
        self.assertEqual(h_tt[0].time_sec, 1)
        self.assertEqual(h_tt[0].cnt_fail, 1)
        self.assertEqual(h_tt[0].cnt_no_result, 0)
        self.assertEqual(h_tt[1].cnt_guess, 3)
        self.assertEqual(h_tt[1].time_sec, 2)
        self.assertEqual(h_tt[1].cnt_fail, 2)
        self.assertEqual(h_tt[1].cnt_no_result, 1)

    def test_hist_ltime_namount_construct(self):
        h_lt = self.word_stat.hist_ltime_nattempt
        stat_game_1 = LTimeAttemptNToCnt(time_sec=1, num_attempts=2,
                                         cnt_guess=1, cnt_fail=0,
                                         cnt_no_result=0)
        histogram_update(h_lt, stat_game_1)
        self.assertEqual(len(list(h_lt)), 1)
        self.assertEqual(h_lt[0].cnt_guess, 1)
        self.assertEqual(h_lt[0].num_attempts, 2)
        self.assertEqual(h_lt[0].time_sec, 1)
        self.assertEqual(h_lt[0].cnt_fail, 0)
        self.assertEqual(h_lt[0].cnt_no_result, 0)

        stat_game_2 = LTimeAttemptNToCnt(time_sec=1, num_attempts=2,
                                         cnt_guess=1, cnt_fail=2,
                                         cnt_no_result=0)
        histogram_update(h_lt, stat_game_2)
        self.assertEqual(len(list(h_lt)), 1)
        self.assertEqual(h_lt[0].cnt_guess, 2)
        self.assertEqual(h_lt[0].num_attempts, 2)
        self.assertEqual(h_lt[0].time_sec, 1)
        self.assertEqual(h_lt[0].cnt_fail, 2)
        self.assertEqual(h_lt[0].cnt_no_result, 0)

        stat_game_3 = LTimeAttemptNToCnt(time_sec=1, num_attempts=3,
                                         cnt_guess=3, cnt_fail=1,
                                         cnt_no_result=0)
        histogram_update(h_lt, stat_game_3)
        self.assertEqual(len(list(h_lt)), 2)
        self.assertEqual(h_lt[0].cnt_guess, 2)
        self.assertEqual(h_lt[0].num_attempts, 2)
        self.assertEqual(h_lt[0].time_sec, 1)
        self.assertEqual(h_lt[0].cnt_fail, 2)
        self.assertEqual(h_lt[0].cnt_no_result, 0)
        self.assertEqual(h_lt[1].cnt_guess, 3)
        self.assertEqual(h_lt[1].num_attempts, 3)
        self.assertEqual(h_lt[1].time_sec, 1)
        self.assertEqual(h_lt[1].cnt_fail, 1)
        self.assertEqual(h_lt[1].cnt_no_result, 0)


if __name__ == "__main__":
    unittest.main()
